#!/bin/sh
#ptheme_gtk - change gtk-theme
#Copyright - GPL 2010,2014,2015,2016,2017
#Sigmund Berglund, Michael Amadio

export TEXTDOMAIN=ptheme
export OUTPUT_CHARSET=UTF-8

#check is X is runnning
[ "`grep -F 'xwin' <<< $(ps -eo pid,command)`" ] && export X_IS_RUNNING=true || export X_IS_RUNNING=false

export WORKDIR=$HOME/.ptheme/tmp/ptheme
[ ! -d $WORKDIR ] && mkdir -p $WORKDIR

TMP="`grep 'gtkrc"' $HOME/.gtkrc-2.0 | awk -F '/gtk-2.0' '{print $1}' | cut -d'"' -f2-`"
ACTIVE_THEME="`basename $TMP`"
IFS='"' read C1 ACTIVE_ICON_THEME C <<< "$(grep -F 'gtk-icon-theme-name' $HOME/.gtkrc-2.0)"
echo -n > $WORKDIR/ptheme_gtkfont

#functions
set_theme_gtk (){
	[ ! "$THEME" ] && export THEME="$ACTIVE_THEME"
	if [ $X_IS_RUNNING = true ]; then
		if [ "$(which gtk-theme-switch2)" ] ; then
			gtk-theme-switch2 -f "$FONT" "/usr/share/themes/$THEME"
		elif [ "$(which switch2)" ] ; then
			switch2 -f "$FONT" "/usr/share/themes/$THEME"
		else
			# from upgrade_ptheme_fix
			PTHEME_GTK=$THEME
			cat > $HOME/.gtkrc-2.0 << _EOF
# -- THEME AUTO-WRITTEN BY gtk-theme-switch2 DO NOT EDIT
include "/usr/share/themes/${PTHEME_GTK}/gtk-2.0/gtkrc"

style "user-font"
{
  font_name=""
}
widget_class "*" style "user-font"

include "/root/.gtkrc-2.0.mine"

# -- THEME AUTO-WRITTEN BY gtk-theme-switch2 DO NOT EDIT
gtk-theme-name = "${PTHEME_GTK}"
_EOF

		fi
	fi
	#icon theme
	if [ "$ICON_THEME" ]; then
		sed -i '/gtk-icon-theme-name/d' $HOME/.gtkrc-2.0
		echo "gtk-icon-theme-name = \"$ICON_THEME\"" >> $HOME/.gtkrc-2.0
	elif [ "$ACTIVE_ICON_THEME" ]; then
		sed -i '/gtk-icon-theme-name/d' $HOME/.gtkrc-2.0
		echo "gtk-icon-theme-name = \"$ACTIVE_ICON_THEME\"" >> $HOME/.gtkrc-2.0
	fi
	#make Qt4 apps theme match gtk2...
	#see also /etc/profile.d/pup_gtk and /root/.config/Trolltech.conf
	THEREBEFORE="$(grep '^gtk-theme-name' /root/.gtkrc-2.0)"
	if [ "$THEREBEFORE" = "" ]; then
		echo "gtk-theme-name=\"${THEME}\"" >> /root/.gtkrc-2.0
		#gtk3 theme support thunor http://murga-linux.com/puppy/viewtopic.php?p=695722#695722
		pathGTK3THEME="`find /usr/share/themes/${THEME} -type d -name gtk-3.0`"
		if [ "$pathGTK3THEME" ]; then #XDG_CONFIG_HOME=/root/.config
			#130404 link to actual theme...
			ln -snf "$pathGTK3THEME" ${XDG_CONFIG_HOME}/gtk-3.0
		fi
	fi
}

set_font (){
	Xdialog --fontsel 0 0 0 2> $WORKDIR/ptheme_gtkfont
	read FONT < $WORKDIR/ptheme_gtkfont
	set_theme_gtk
}

export -f set_theme_gtk set_font

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-t) export THEME="$2"
				[ ! "$THEME" ] && export THEME="$ACTIVE_THEME"
				shift;;
			-f) export FONT="$2"; shift;;
			-i) export ICON_THEME="$2"
				shift;;
			-h|--help)
echo 'Usage: ptheme_gtk [OPTIONS]

Options
  -f FONT       Set font for theme
  -i ICON_THEME Set icon-theme for theme
  -t THEME      Set theme without open gui 
  -h            Show this help message'; exit;;
		esac
		shift
		I=$[$I+1]
	done
done
if [ "$THEME" ]; then
	set_theme_gtk
	exit
fi

#build list
ls -1A /usr/share/themes > $WORKDIR/ptheme_gtkthemes
while read I; do ITEMS="$ITEMS<item>$I</item>"; done < $WORKDIR/ptheme_gtkthemes
#icon themes
while read I; do
	MENUITEMS_ICONS_GTK="$MENUITEMS_ICONS_GTK<menuitem label=\"$I\"><action>echo $I > $WORKDIR/ptheme-gtkicon</action><action>refresh:ICON_THEME</action></menuitem>"
done <<< "$(find /usr/share/icons -type f -maxdepth 2 -name 'index.theme' | sed -e 's/^/"/' -e 's/$/"/' | xargs grep 'Name=' | cut -d '=' -f2)"
[ ! "$ICON_THEME" ] && ICON_THEME="$ACTIVE_ICON_THEME"
echo "$ICON_THEME" > $WORKDIR/ptheme-gtkicon


export pThemeGTK='
<window title="GTK '$(gettext 'theme switcher')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed "puppy_theme.svg" 60 "$(gettext 'A GTK-theme defines how widgets in programs shows up. This is buttons, lists, fields, and so on. Please apply a theme from the list.')"`"' 
  <frame '$(gettext 'Preview')'>
    <menubar>
      <menu>
        <menuitem><label>'$(gettext 'Simple item')'</label></menuitem>
        <menuitem><label>'$(gettext 'Simple item')'</label></menuitem>
        <separator></separator>
        <menuitem icon="gtk-convert"><label>'$(gettext 'With image')'</label></menuitem>
        <menuitem icon="gtk-convert"><label>'$(gettext 'With image')'</label></menuitem>
        <label>'$(gettext 'Menu')'</label>
      </menu>
    </menubar>
    <hbox>
      <checkbox><default>true</default><label>'$(gettext 'Check')' 1</label></checkbox>
      <checkbox><label>'$(gettext 'Check')' 2</label></checkbox>
      <text><label>"  "</label></text>
      <radiobutton><label>'$(gettext 'Radio')' 1</label></radiobutton>
      <radiobutton><label>'$(gettext 'Radio')' 2</label></radiobutton>
      <radiobutton><label>'$(gettext 'Radio')' 3</label></radiobutton>
    </hbox>
    <hbox>
      <button><label>'$(gettext 'Button')'</label><input file stock="gtk-execute"></input></button>
      <text><label>"  "</label></text>
      <checkbox draw_indicator="false"><default>true</default><label>" '$(gettext 'Toggle')' 1 "</label></checkbox>
      <checkbox draw_indicator="false"><label>" '$(gettext 'Toggle')' 2 "</label></checkbox>
      <text><label>"  "</label></text>
      <button><label>'$(gettext 'Disabled')'</label><input file stock="gtk-close"></input><visible>disabled</visible></button>
    </hbox>
    <hbox>
      <progressbar><input>echo 40; echo "(40%)"</input></progressbar>
      <combobox>
       <item>'$(gettext 'item')' A</item>
       <item>'$(gettext 'item')' B</item>
       <item>'$(gettext 'item')' C</item>
      </combobox>
    </hbox>
  </frame>
  <vbox space-expand="true" space-fill="true">
    <frame '$(gettext 'Define Theme')'>  
      <tree rules_hint="true" space-expand="true" space-fill="true">
        <label>'$(gettext 'Themes')'</label>
        <variable>THEME</variable>
        <height>200</height><width>50</width>
        '$ITEMS'
        <action signal="button-release-event">set_theme_gtk</action>
      </tree>
      <hbox>
        <menubar space-expand="false" space-fill="false">
          <menu label="'$(gettext 'Icons theme')'" stock="gtk-arrow_down">
            '$MENUITEMS_ICONS_GTK'
            <menuitem label="">
              <action>echo "" > $WORKDIR/ptheme-gtkicon</action>
              <action>refresh:ICON_THEME</action>
            </menuitem>
          </menu>
        </menubar>
        <entry editable="false" width-request="150" space-expand="true" space-fill="true">
          <variable>ICON_THEME</variable>
          <input>cat '$WORKDIR'/ptheme-gtkicon</input>
        </entry>
      </hbox>
      <hbox>
        <button>
          <label>'$(gettext 'Font')'</label>
          <input file stock="gtk-bold"></input>
          <action>set_font</action>
          <action>refresh:FONT</action>
        </button>
        <entry editable="false" secondary-icon-stock="gtk-clear">
          <variable>FONT</variable>
          <input>cat '$WORKDIR'/ptheme_gtkfont</input>
          <action signal="secondary-icon-release">echo > '$WORKDIR'/ptheme_gtkfont; export FONT=""; set_theme_gtk</action>
          <action signal="secondary-icon-release">refresh:FONT</action>
        </entry>
      </hbox>
    </frame>
  </vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon internet`"'
      <label>'$(gettext 'More themes')'</label>
      <action>defaulthtmlviewer http://www.murga-linux.com/puppy/viewtopic.php?t=30075 &</action>
    </button>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon puppy_theme.svg`"'
      <label>" '$(gettext 'Global theming')' "</label>
      <action>/usr/sbin/ptheme</action>
    </button>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
      <action>ptheme_gtk -t '$ACTIVE_THEME' &</action>
      <action>EXIT:exit</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <label>'$(gettext 'Ok')'</label>
      <action>EXIT:exit</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
</window>'
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p pThemeGTK 2> /dev/null
